<?php
    define('OF_DIRECTORY', get_stylesheet_directory_uri());

    if (current_user_can('add_users')) {
        // add the admin menu item
        add_action('admin_menu', 'register_custom_admin_menu');

        // enqueue scripts and styles
        add_action('admin_head', 'enqueue_styles_and_scripts');
    }


    // Utility Functions
    // ------------------------------

    function register_custom_admin_menu() {
        // add the base menu
        add_menu_page(
            'Natural Theme Bridge Installer',
            'Bridge',
            'add_users',
            'bridge-installer',
            'getPage_install_theme',
            OF_DIRECTORY . '/images/icons/icon-16x16.png'
        );
    }

    function enqueue_styles_and_scripts() {
        wp_enqueue_style('style-admin', OF_DIRECTORY . '/style-admin.css');
    }


    // Page Getters
    // ------------------------------

    function getPage_install_theme() {
        get_template_part('bridge', 'install');
    }


    // Ajax stuff
    // ------------------------------

    function BridgeInstall() {
        // sleep(3);

        $copy_src  = $_POST['copy_src'];
        $copy_dest = $_POST['copy_dest'];

        try {
            shell_exec("cp -r $copy_src $copy_dest");

            $result = 'success';
        } catch (Exception $e) {
            $result = 'fail';
        }

        die($result);
    }

    add_action('wp_ajax_nopriv_BridgeInstall', 'BridgeInstall');
    add_action('wp_ajax_BridgeInstall', 'BridgeInstall');
?>